<?php
/**
 * @name /www/unit-tests/NeededItem.php
 * @author Kimberly Berger <knb4850@esu.edu>
 * @version  2011.03.30

 *
 * Unit test for Item class.
 */

// Sets up configuration settings and includes libraries.
require_once('_ini.php');
// Libraries
ReqLib('/lib/private/NeededItem.php');
ReqLib('/lib/private/Item.php');

function DefineHeader() {
?><style type="text/css">
  code {
    font: bold 10pt 'Courier New';
    color: #666;
    background-color: #CCC;
    border: 3px solid #666;
    padding: 4px 6px;
    margin: 10px 20px;
    display: block;
  }
  form, ul, ol {
    margin: 0px 20px;
  }
  a#Back {
    float: right;
    margin: 10px;
    padding: 4px 6px;
    border: 1px solid #666;
    background-color: #CCC;
  }
</style><?
}

/**
 * Defines page content.
 *
 */
function DefineContent() {
/* Methods:

   NeededItem

  "GET":
  NeededItem[] GetNeededItems(int offset, int maxResults)
  bool Exists(int NeededItemId)
  string ToString()

  "SCUD":
  bool Load(int NeededItemId)
  bool Create(ItemId, OrphanageId)
  bool Save()
  static bool Delete(int NeededItemId)

  Custom:
  GetNeededItemIdsByItemId(ItemId)
  GetNeededItemIdsByOrphanageId(OrphanageID)
  GetNeededItemIdByOrphanageAndItemId($OrphanageId, $ItemId)

*/
  $actions = array(
    'Delete(id)',
    'Delete All',
    'Create',
    'View Details',
    'List(offset,max)',
    'GetIdsByItemId(id)',
    'GetIdsByOrphanageId(id)',
	'GetIdsByItemAndOrphanageId(Orphid,Itemid)',
    'Save',
  );
  // Page content goes here.
  ?>
<h1><a href="NeededItem.php">NeededItem - Unit Test</a></h1>
<a id="Back" href="index.php">Back to Unit Testing Home</a>
<form method="GET">
  <label for="Param1">Param 1: </label><input type="text" value="<?=htmlspecialchars($_GET['Param1']) ?>" name="Param1" /><br />
  <label for="Param2">Param 2: </label><input type="text" value="<?=htmlspecialchars($_GET['Param2']) ?>" name="Param2" /><br />
<?
  foreach ($actions as $action)
    println('<input type="submit" name="Action" value="' . $action . '" /><br />');
?><br />
<hr />
<?
  DoAction();
?></form>
<?
}

function DoAction() {
?><p>
<?
  switch ($_GET['Action']) {
    case 'Delete(id)':
      println(NeededItem::Delete($_GET['Param1']) ? 'Deleted' : 'Not deleted');
      break;

    case 'Delete All':
      $nis = NeededItem::GetNeededItems();
      foreach ($nis as $ni)
        NeededItem::Delete($ni->NeededItemId);
      println('Data cleared.');
      break;

    case 'Create':
      $ni = new NeededItem();
      $nis = array(
        array(rand(1,10), rand(1,10)),
        array(rand(1,10), rand(1,10)),
        array(rand(1,10), rand(1,10)),
        array(rand(1,10), rand(1,10)),
        array(rand(1,10), rand(1,10))
      );
      foreach ($nis as $data)
        println("Creating neededitems (ItemID:'{$data[0]}'/OrphanageID:'{$data[1]}'): " . ($ni->Create($data[0], $data[1]) ? 'Created' : 'Not created') . '<br />');
      break;

    case 'List(offset,max)':
	  println('Displays the NeededItemId and the corresponding ItemName');
      println('<ol>');
      foreach(NeededItem::GetNeededItems($_GET['Param1'], $_GET['Param2']) as $ni)
        println("<li>{$ni->NeededItemId}: {$ni->ItemObject->Name}, {$ni->OrphanageObject->Name} </li>");
      println('</ol>');
      break;

    case 'View Details':
      println('<textarea style="width: 600px; height: 400px;">' . print_r(NeededItem::GetNeededItems(0, 1), true) . '</textarea>');
      break;

    case 'GetIdsByItemId(id)':
      println('<textarea style="width: 600px; height: 400px;">'. "ByItemId ('" . htmlspecialchars($_GET['Param1']) . "') = '" . print_r((NeededItem::GetNeededItemIdsByItemId($_GET['Param1'])), true) . "' " .  '</textarea>');
	  break;

	  case 'GetIdsByOrphanageId(id)':
	  println('<textarea style="width: 600px; height: 400px;">'. "ByItemId ('" . htmlspecialchars($_GET['Param1']) . "') = '" . print_r((NeededItem::GetNeededItemIdsByOrphanageId($_GET['Param1'])), true) . "' " .  '</textarea>');
      break;

	  case 'GetIdsByItemAndOrphanageId(Orphid,Itemid)':
	   println('<textarea style="width: 600px; height: 400px;">'. "ByItemId and OrphanageId('" . htmlspecialchars($_GET['Param1']) . "', '" . htmlspecialchars($_GET['Param2']) . "') = '" . print_r((NeededItem::GetNeededItemIdsByOrphanageAndItemId($_GET['Param1'], $_GET['Param2'])), true) . "' " .  '</textarea>');
       break;

    case 'Save':
      $ni = NeededItem::GetNeededItems(0, 1);
      if (!count($ni)) {
        $ni = new NeededItem();
        $ni->Create(rand(1,10), rand(1,10));
      }
      else
       $ni = $ni[0];
      $ni->NeededQuantity= rand(1, 100);
      $ni->NeededNotes = rand(1, 100);
      $ni->DateRequested = date('Y-m-d', mktime(0,0,0, rand(1,12), rand(1,28),rand(1000, 5000)));
      $ni->QuantitySent = rand(1, 100);
	  $ni->ItemId = rand(1,10);
      $ni->OrphanageId = rand(1, 10);
	  if($ni->Save()){
		println('Saved');
        println('<br /><textarea style="width: 600px; height: 400px;">NeededQuantity: ' . $ni->NeededQuantity . "\r\n" . print_r($ni->ToString("\r\n"), true) . '</textarea>');
	  }
	  else
		println('Not Saved');
      break;

      default:
      break;
  }
?></p>
<?
}

// Launches the page; without this, nothing happens.
require_once('_loader.php');